﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Omu.ValueInjecter;
using Xiucai.BPM.Core;
using Xiucai.Common;
using Xiucai.Common.Data;
using Xiucai.Common.Data.Filter;
using Xiucai.BPM.Core.Bll;
using Xiucai.BPM.Core.Model;

namespace Xiucai.BPM.Admin.status_xqh.ashx
{
    /// <summary>
    /// DataTestHandler 的摘要说明
    /// </summary>
    public class XQHHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (SysVisitor.Instance.IsGuest)
            {
                context.Response.Write(
                    new JsonMessage { Success = false, Data = "-99", Message = "登录已过期，请重新登录" }.ToString()
                    );
                context.Response.End();
            }

            var json = HttpContext.Current.Request["json"];
            var rpm = new RequestParamModel<XqhData>(context) { CurrentContext = context, Action = context.Request["Action"] };
            if (!string.IsNullOrEmpty(json))
            {
                rpm = JSONhelper.ConvertToObject<RequestParamModel<XqhData>>(json);
                rpm.CurrentContext = context;
            }
            int k = 0;
            switch (rpm.Action)
            {
                case "xqhNums":
                    int uid = PublicMethod.GetInt(rpm.Request("uid"));
                        if(uid==0)
                        context.Response.Write(DevConfigBll.Instance.XQHShortNames(SysVisitor.Instance.CurrentUser.KeyId));
                    else
                            context.Response.Write(DevConfigBll.Instance.XQHShortNames(uid));

                    
                    break;
                case "phone":                   
                    string phone = PublicMethod.GetString(rpm.Request("devID"));
                    context.Response.Write(JsonDataPhone(phone, rpm.Pageindex, rpm.Pagesize, rpm.Filter));
                    break;
                case "phoneavg":
                    string phoneavg = PublicMethod.GetString(rpm.Request("devID"));
                    context.Response.Write(JsonDataPhoneAvg(phoneavg, rpm.Pageindex, rpm.Pagesize, rpm.Filter));
                    break;
                case "mavg":
                    string mavg = PublicMethod.GetString(rpm.Request("devID"));
                    context.Response.Write(JsonDataMAvg(mavg, rpm.Pageindex, rpm.Pagesize, rpm.Filter));
                    break;
                default:
                    var devID = PublicMethod.GetInt(rpm.Request("devID"));
                    context.Response.Write(JsonDataForEasyUIdataGrid(devID, rpm.Pageindex, rpm.Pagesize, rpm.Filter));
                    break;
            }
        }


        private string JsonDataForEasyUIdataGrid(int devID, int pageindex, int pagesize, string filterJSON)
        {
            //数据权限条件
            string where = "devID=" + devID;

            //if (SysVisitor.Instance.IsAdmin)
            //{
            //}
            //else
            //{
            if (filterJSON != "" && filterJSON != "\"\"")
                {
                    where = FilterTranslator.ToSql(filterJSON) + " and " + where;
                }
            //}
            var pcp = new ProcCustomPage("t_xqhStatus")
            {
                KeyFields = "keyid",
                PageIndex = pageindex,
                PageSize = pagesize,
                OrderFields = "keyid desc",
                WhereString = where
            };
            int recordCount;
            DataTable dt = DbUtils.GetPageWithSp(pcp, out recordCount);
            return JSONhelper.FormatJSONForEasyuiDataGrid(recordCount, dt);
        }
        private string JsonDataPhone(string phone, int pageindex, int pagesize, string filterJSON)
        {
            //数据权限条件
            string where = "";
            if (filterJSON != "" && filterJSON != "\"\"")
            {
                where = FilterTranslator.ToSql(filterJSON);
            }
            var pcp = new ProcCustomPage(phone)
            {
                KeyFields = "ID",
                PageIndex = pageindex,
                PageSize = pagesize,
                OrderFields = "datetime desc",
                WhereString = where
               
            };
            int recordCount;
            DataTable dt = DbUtils.GetPageWithSpPhone(pcp, out recordCount);
            return JSONhelper.FormatJSONForEasyuiDataGrid(recordCount, dt);
        }
        private string JsonDataPhoneAvg(string phone, int pageindex, int pagesize, string filterJSON)
        {
            //数据权限条件
            string where = "";
            if (filterJSON != "" && filterJSON != "\"\"")
            {
                where = FilterTranslator.ToSql(filterJSON);
            }
            else
            {
                where = "1=1";
            }
            var pcp = new ProcCustomPage(phone)
            {
                KeyFields = "datetime",
                PageIndex = pageindex,
                PageSize = pagesize,
                OrderFields = "datetime desc",
                WhereString = where,
                Sp_PagerName = "ProcCustomPageAvg"

            };
            int recordCount;
            DataTable dt = DbUtils.GetPageWithSpPhone(pcp, out recordCount);
            return JSONhelper.FormatJSONForEasyuiDataGrid(recordCount, dt);
        }

        private string JsonDataMAvg(string phone, int pageindex, int pagesize, string filterJSON)
        {
            //数据权限条件
            string where = "";
            if (filterJSON != "" && filterJSON != "\"\"")
            {
                where = FilterTranslator.ToSql(filterJSON);
            }
            var pcp = new ProcCustomPage(phone)
            {
                KeyFields = "datetime",
                PageIndex = pageindex,
                PageSize = pagesize,
                OrderFields = "datetime desc",
                WhereString = where,
                Sp_PagerName = "ProcCustomPageMAvg"

            };
            int recordCount;
            DataTable dt = DbUtils.GetPageWithSpPhone(pcp, out recordCount);
            return JSONhelper.FormatJSONForEasyuiDataGrid(recordCount, dt);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }




}